
import axios from 'axios'
const createApiClient = (baseURL = '') => {
  const instance = axios.create({
    baseURL,
    timeout: 10000,
    headers: {
      'Content-Type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest'
    }
  })

  // 增强型 POST 方法
  const hDoPost = (url, params, data) => {
    return instance.post(url, data, { params })
      .then(response => {
        // 统一处理成功响应
        if (response.status === 200) {
          return response.data // 直接返回业务数据
        }
        return Promise.reject(new Error(`请求失败，状态码：${response.status}`))
      })
      .catch(error => {
        // 统一错误日志
        console.error('[API Error]', error.config.url, error.message)
        return Promise.reject(error)
      })
  }

  return {
    hDoPost
  }
}

export default createApiClient